$(function () {

    var supplier_datagrid = $("#supplier_datagrid")
    //dialog的表单
    var supplier_add_form = $("#supplier_add_form")
    //新增供应商的dialog
    var supplier_add_dialog = $("#supplier_add_dialog")

    supplier_datagrid.datagrid({
        url:"/supplier/list",
        fit:true,
        fitColumns:true,
        rownumbers:true,
        singleSelect:true,
        columns:[[
            {field:"name",title:"供应商",width:100},
            {field:"address",title:"地址",width:100},
            {field:"arrears",title:"应付欠款",width:100},
            {field:"refundment",title:"应收退款",width:100},
            {field:"contactsMan",title:"联系人",width:100},
            {field:"tel",title:"联系电话",width:100},
            {field:"inputTime",title:"添加时间",width:100},
            {field:"operator",title:"操作人员",width:100,formatter:formatterOperator},
            // {filed: 'opt', title: '操作', width: 100,formatter:formatterOperate}
        ]],
        toolbar:"#supplier_dialog",
        onLoadSuccess:function (data) {
            console.log(data);
        }
    })

    cmdObj = {
        //新增供应商
        supplier_add:function () {

            supplier_add_form.form("clear");
            supplier_add_dialog.dialog("setTitle","新增供应商")
            supplier_add_dialog.dialog("open")
        },

        //保存修改
        supplier_add_save:function () {
             var url = "supplier/save";
             var idValue = $("#supplier_add_form :input[name='id']").val();
            console.log(idValue);
            //判断是编辑还是回显
            if (idValue){
                url = "/supplier/update";
            }

            supplier_add_form.form("submit",{
                url:url,
                success:function (data) {
                    data = $.parseJSON(data);
                    if (data.success){
                        $.messager.alert("温馨提醒","保存成功","info",function () {
                            supplier_datagrid.datagrid("reload");
                            supplier_add_dialog.dialog("close")
                        })
                    }else{
                        $.messager.alert("温馨提醒",data.msg,"error")
                    }
                }
            })


        },
        //刷新
        supplier_reload:function () {
            supplier_datagrid.datagrid("reload");
        },
        //删除
        supplier_delete:function () {
            var selectedRow = supplier_datagrid.datagrid("getSelected");
            if(!selectedRow){
                $.messager.alert("温馨提醒","请先选中行","error");
                return;
            }
            $.messager.alert("温馨提醒","确定要删除吗","info",function () {

                $.post("/supplier/delete",{id:selectedRow.id},function (data) {
                    if(data.success){
                        $.messager.alert("温馨提醒","删除成功","info",function () {
                            supplier_datagrid.datagrid("reload");
                            supplier_add_dialog.dialog("close")
                        })
                    }else{
                        $.messager.alert("温馨提醒",data.msg,"error")
                    }
                })
            })
        },
        //取消
        cancel:function () {
            supplier_add_dialog.dialog("close")
        },
        //修改
        supplier_edit:function () {
            supplier_add_dialog.dialog("setTitle","修改供应商")
            var selectedRow = supplier_datagrid.datagrid("getSelected");
            if(!selectedRow){
                $.messager.alert("温馨提醒","请先选中行","error");
                return;
            }
            supplier_add_form.form("load",selectedRow)
            supplier_add_dialog.dialog("open")

        }
    }
    
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        cmdObj[cmd]();
    })
    
})

function formatterOperator(value,row,index) {

    return value ?value.username :"";
}

function formatterOperate(value,row,index) {
    var Action = "<a href='javascript:void(0);' onclick='Edit(" + row.ID + ")'>修改</a>\
                        | <a href='javascript:void(0);' onclick='Delete(" + row.ID + ")'>删除</a>";
    return Action;
}